package DemoText;

/**
 * 解码方法
 */
public class LeetCode91 {
    public static int numDecodings(String s) {
        char[] nums = s.toCharArray();
        int n = nums.length;
        int[] dp = new int[n];
        if (nums[0] != '0') dp[0] = 1;
        if (n == 1) return dp[0];

        if (nums[1] != '0') dp[1] += dp[0];
        int t = (nums[0] - '0') * 10 + nums[1] - '0';
        if (t >= 10 && t <= 26) dp[1] += 1;
        for (int i = 2; i < n; i++) {
            if (nums[i] != '0') dp[i] += dp[i - 1];
            int tt = (nums[i - 1] - '0') * 10 + nums[i] - '0';
            if (tt >= 10 && tt <= 26) dp[i] += dp[i - 2];
        }
        return dp[n - 1];
    }

    public static void main(String[] args) {
        String s = "2101";
        System.out.println(numDecodings(s));
    }
}
